﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Model;
using IBLL;
using BLL;
using BllFactory;

namespace ETWebApi2.Models
{
    public class PermissionsHandler
    {

        private ILocks_tbService lockServer = ServiceFactory.CreatService<ILocks_tbService>();
        private ICustomers_tbService cusServer = ServiceFactory.CreatService<ICustomers_tbService>();
        /// <summary>
        /// 权限验证 需要下载的时候有用到
        /// </summary>
        /// <returns></returns>
        public  string Authorize (int id)
        {
            string LockInNum =HttpContext.Current.Session["LockInNum"] as string;
            string result = "noRights";
            if (!string.IsNullOrEmpty(LockInNum))
            {
                Locks_tb sItem = lockServer.GetList(l=>l.lockInsideNum==LockInNum).FirstOrDefault();
                if (sItem != null && sItem.lstate != 3)//锁存在且没有被废弃
                {
                    Customers_tb cItem = cusServer.GetList(c=>c.Id==sItem.customerId).FirstOrDefault();
                    if (cItem != null && cItem.IsDeal == 11)//目前只考虑此客户确实存在即可 （该客户能与哪些其他客户交换文件以后再完善）
                    {
                        result = "canDownload";//只能下载
                    }
                    else if (cItem != null && cItem.IsDeal == 12)
                    {
                        result = "allRights";  //全部权限                     
                    }
                    else
                    {
                        result = "noRights";
                        //HttpContext.Current.Session.Abandon();//取消当前会话 并返回登录
                    }
                }
                return result;
            }
            else
            {
                    Customers_tb cItem = cusServer.GetList(c=>c.Id== id).FirstOrDefault();
                    if (cItem != null && cItem.IsDeal > 10)
                    {
                        result = "canDownload";//只能下载
                        return result;
                    }
                    else
                    {
                        result = "noRights";
                        //HttpContext.Current.Session.Abandon();//取消当前会话 返回个人信息页（）
                        return result;
                    }
            }

        }
    }
}